package com.zlp.controller;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class FutureTask {

    private static ExecutorService executor = Executors.newFixedThreadPool(16);

    @SneakyThrows
//    @Transactional(rollbackFor = Exception.class)
    public static void main(String[] args) {

        long start = System.currentTimeMillis();

//        ArrayList<OGStatusDTO> ogStatusDTOS1 = new ArrayList<>();
//        ArrayList<OGStatusDTO> ogStatusDTOS2 = new ArrayList<>();

        List<CompletableFuture<Void>> completableFutureList = new ArrayList<>();


        CompletableFuture<Void> clickFuture = CompletableFuture.runAsync(() -> {
            for (int i = 0; i < 5; i++) {
                getOGStatusDTO();
            }
        }, executor);
        completableFutureList.add(clickFuture);
        CompletableFuture<Void> voidCompletableFuture = null;
        for (CompletableFuture<Void> completableFuture : completableFutureList) {

            voidCompletableFuture = CompletableFuture.allOf(completableFuture);
        }

        // 全部执行完成一起提交
        voidCompletableFuture.get();
        long end = System.currentTimeMillis();
        log.info("ogStatusDTOS1:{}", "ogStatusDTOS1");
        log.info("执行时间===>time:{}", end - start);






        // 没有异步执行
        long start1 = System.currentTimeMillis();
        for (int i = 0; i < 5; i++) {
            getOGStatusDTO();
//            ogStatusDTOS2.add(getOGStatusDTO());
        }
        long end1 = System.currentTimeMillis();
        log.info("ogStatusDTOS2:{}", "ogStatusDTOS2");
        log.info("执行时间===>time:{}", end1 - start1);


    }


    @SneakyThrows
    private static Integer shareCount() {
        Thread.sleep(500L);
        return 1;
    }

    @SneakyThrows
    private static String upCount() {
        Thread.sleep(800L);
        return "455";
    }

    @SneakyThrows
    private static String clickCount() {
        Thread.sleep(100L);
        return "123";
    }


    @SneakyThrows
    private static Integer getOGStatusDTO() {
        Thread.sleep(1000L);
//        OGStatusDTO ogStatusDTO = new OGStatusDTO();
//        ogStatusDTO.setFBillNo(clickCount());
//        ogStatusDTO.setOrderCode(upCount());
//        ogStatusDTO.setNowStatus(shareCount());
        return 0;
    }


}
